#include "LogUtil.h"  



LogUtil* pLog = NULL;

LogUtil* LogUtil::getInstance() {
	if (pLog == NULL) {
		pLog = new LogUtil();
		pLog->init();
	}
	return pLog;
}

void LogUtil::init()
{
	string dirPath = FileUtils::getInstance()->getWritablePath() + "logs/";
	if (!FileUtils::getInstance()->isDirectoryExist(dirPath))
	{
		FileUtils::getInstance()->createDirectory(dirPath);
	}
}

void LogUtil::log(const char* content)
{
	if (LOG_ENABLE)
	{
		saveFile(combine(content).c_str(), "logs/", StringUtils::format("%s.log", getCurrentDate().c_str()));
	}
}

bool LogUtil::saveFile(const char* pContent, std::string dir, std::string fileName) {

	string dirPath = FileUtils::getInstance()->getWritablePath() + dir;
	string path = dirPath + fileName;

	if (!logFileCheck(fileName.c_str()))
	{
		string metaFilePath = FileUtils::getInstance()->getWritablePath() + "meta";
		cocos2d::ValueVector fileList = FileUtils::getInstance()->getValueVectorFromFile(metaFilePath);
		if (fileList.size() > 15) 
		{
			FileUtils::getInstance()->removeFile(dirPath + fileList.front().asString());
			fileList.erase(fileList.begin());
		}
		fileList.push_back(Value(fileName));
		FileUtils::getInstance()->writeValueVectorToFile(fileList, metaFilePath);
	}

	FILE* file = fopen(path.c_str(), "at+");
	if (file) {
		fputs(pContent, file);
		fputs("\n", file);

		fclose(file);
	}
	else
		//CCLOG("save file error.\n");

	return true;
}

std::string LogUtil::combine(const char* pContent) {

	return StringUtils::format("[%s] %s", getCurrentTime().c_str(), pContent);
}

std::string LogUtil::getCurrentDate() 
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

	struct timeval now;
	struct tm* time;

	gettimeofday(&now, NULL);
	time = localtime(&now.tv_sec);
	int year = time->tm_year + 1900;

	char date[32] = { 0 };
	sprintf(date, "%d%02d%02d", (int)time->tm_year + 1900, (int)time->tm_mon + 1, (int)time->tm_mday);
	return StringUtils::format("%s", date);

#endif

#if ( CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )

	struct tm* tm;
	time_t timep;
	time(&timep);

	tm = localtime(&timep);
	char date[32] = { 0 };
	sprintf(date, "%d%02d%02d", (int)tm->tm_year + 1900, (int)tm->tm_mon + 1, (int)tm->tm_mday);
	return StringUtils::format("%s", date);

#endif
}

std::string LogUtil::getCurrentTime()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

	struct timeval now;
	struct tm* time;

	gettimeofday(&now, NULL);
	time = localtime(&now.tv_sec);
	int year = time->tm_year + 1900;

	char date[32] = { 0 };
	sprintf(date, "%d%02d%02d %02d:%02d:%02d", (int)time->tm_year + 1900, (int)time->tm_mon + 1, (int)time->tm_mday
		, (int)time->tm_hour, (int)time->tm_min, (int)time->tm_sec);
	return StringUtils::format("%s", date);

#endif

#if ( CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )

	struct tm* tm;
	time_t timep;
	time(&timep);

	tm = localtime(&timep);
	char date[32] = { 0 };
	sprintf(date, "%d%02d%02d %02d:%02d:%02d", (int)tm->tm_year + 1900, (int)tm->tm_mon + 1, (int)tm->tm_mday
		, (int)tm->tm_hour, (int)tm->tm_min, (int)tm->tm_sec);
	return StringUtils::format("%s", date);

#endif
}

bool LogUtil::logFileCheck(const char* fileName)
{
	string metaFilePath = FileUtils::getInstance()->getWritablePath() + "meta";
	cocos2d::ValueVector fileList = FileUtils::getInstance()->getValueVectorFromFile(metaFilePath);
	for (auto it = fileList.begin(); it < fileList.end(); it++) 
	{
		if (strcmp((*it).asString().c_str(), fileName) == 0) 
		{
			return true;
		}
	}
	return false;
}
